import MyThrowable.InputWrongful;
import student.StudentList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2022-12-14
 * Time: 6:41
 */
public class Main {
    static String PatternStr = "^[\\u4e00-\\u9fa5]{2,10}$";
    static String PatternStr1 = "^[0-6]+$";
    public static User login(){
        while (true){
            System.out.println("请输入你的姓名(限制汉字个数2~10)：");
            Scanner scan = new Scanner(System.in);
            String userName = scan.nextLine();
            try{
                if(Pattern.matches(PatternStr,userName)){
                    while (true) {
                        System.out.println("请输入你的身份(0/1)：1-》教师，0-》学生");
                        String ch = scan.nextLine();
                        if(!Pattern.matches(PatternStr1,ch)){
                           try {
                               throw new InputWrongful("无法匹配,请重新输入!");
                           }catch (InputWrongful e){
                               e.printStackTrace();
                           }
                        }else {
                            if (Integer.valueOf(ch) == 1) {
                                return new AdminUser(userName);
                            } else if (Integer.valueOf(ch) == 0) {
                                return new NormalUser(userName);
                            } else {
                                System.out.println("无法匹配,请重新输入!");
                            }
                        }
                    }
                }else {
                    throw new InputWrongful("输入有误,请再次输入!");
                }
            }catch (InputWrongful e){
                e.printStackTrace();
            }
        }
    }
    public static void main(String [] args){
        StudentList studentList = new StudentList();
        User user = login();
        while (true){
            try {
                int choice = user.menu();
                if(!Pattern.matches(PatternStr1,String.valueOf(choice))){
                    throw new InputWrongful("输入有误,请再次输入!");
                }
                user.doOperation(choice, studentList);
            }catch (InputWrongful e) {
                e.printStackTrace();
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("输入有误,请再次输入!");
            }
        }
    }
}
